package es.tikitaka.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;

import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import es.tikitaka.dao.PerfilDicService;
import es.tikitaka.dictionary.PerfilDic;

/**
 * 
 * @author Saúl Domínguez
 * @since 2010
 * @category PFC
 */
@Name("perfilService")
public class PerfilDicServiceImpl implements PerfilDicService {

	@In
	EntityManager em;

	public PerfilDicServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("unchecked")
	@Begin(join = true) 
	public List<PerfilDic> getList() {
		List<PerfilDic> list = null;
		try {
			list = em
					.createQuery("select p from PerfilDic p order by p.perfil")
					.getResultList();
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Begin(join = true)
	public PerfilDic obtenerPerfil(String perfil) {
		PerfilDic per = null;
		try {
			per = (PerfilDic) em.createQuery(
					"select p from PerfilDic p where p.perfil =:pf")
					.setParameter("pf", perfil).getSingleResult();
		} catch (RuntimeException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return per;
	}

	public boolean delete(Integer id) {
		boolean correcto;
		try {
			PerfilDic perfil = em.getReference(PerfilDic.class, id);
			em.remove(perfil);
			em.flush();
			correcto = true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			correcto = false;
		}
		return correcto;
	}

	public boolean save(PerfilDic perfil) {
		boolean correcto;
		try {
			em.persist(perfil);
			em.flush();
			correcto = true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			correcto = false;
		}
		return correcto;
	}
}
